home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / cli / shelltools.lha / touch / touch.c next >
Encoding:
C/C++ Source or Header  |  1992-04-12  |  2.0 KB  |  98 lines

  1. ;/* touch - compiles with SAS/C 5.10a by executing this file
  2. lc -cfis -v -b0 -O -j73 touch
  3. blink touch.o to touch
  4. quit ;*/
  5. /*
  6. *    touch.c - touches files (makes their date NOW) - v1.0
  7. *
  8. *    Usage:    touch <file-list>
  9. *
  10. *    Handles standard AmigaDOS wildcards. If a given file doesn't exist (and
  11. *    it's not a pattern) the file is created with size zero.
  12. *
  13. *    Martin W. Scott, 4/92.
  14. */
  15. #include <exec/types.h>
  16. #include <libraries/dos.h>
  17. #include <dos/dosasl.h>
  18. #include <dos/rdargs.h>
  19.  
  20. #include <proto/exec.h>        /* use PRAGMAS */
  21. #include <proto/dos.h>
  22.  
  23. void    createfile(char *), touch(char *),
  24.     touchall(char *), main(void);
  25.  
  26. char version_str[] = "$VER: touch v1.0";
  27.  
  28. struct DosLibrary *DOSBase;
  29. struct DateStamp now;            /* indeed */
  30. struct AnchorPath __aligned ap;        /* for Match(First|Next|End) */
  31.  
  32.  
  33. void main(void)        /* entry: touch files (set their date to NOW) */
  34. {
  35.     struct RDArgs *readargs;
  36.     LONG rargs[1];
  37.     char **strings;
  38.  
  39.     if (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L))
  40.     {
  41.         if (readargs = ReadArgs("FILES/A/M", rargs, NULL))
  42.         {
  43.             strings = (char **)(rargs[0]);
  44.  
  45.             DateStamp(&now);
  46.             while (*strings)
  47.             {
  48.                 touchall(*strings);
  49.                 strings++;
  50.             }
  51.         }
  52.         else PrintFault(IoErr(), "touch");
  53.  
  54.         CloseLibrary(DOSBase);
  55.     }
  56.  
  57. } /* main */
  58.  
  59.  
  60. void createfile(char *file)        /* touch named file in current directory */
  61. {
  62.     BPTR fh;
  63.  
  64.     if (fh = Open(file, MODE_NEWFILE))
  65.         Close(fh);
  66.     else PrintFault(IoErr(), "touch");
  67. }
  68.  
  69. void touch(char *file)        /* touch named file in current directory */
  70. {
  71.     if (!SetFileDate(file, &now))
  72.         PrintFault(IoErr(), "touch");
  73. }
  74.  
  75. void touchall(char *pat)    /* touch all files matching pattern */
  76. {
  77.     BPTR oldcd;
  78.     LONG err;
  79.  
  80.     if ((err = MatchFirst(pat, &ap)) == 0)    /* success */
  81.     {
  82.         do {
  83.             oldcd = CurrentDir(ap.ap_Current->an_Lock);
  84.             touch(ap.ap_Info.fib_FileName);
  85.             CurrentDir(oldcd);
  86.             err = MatchNext(&ap);
  87.         } while (!err);
  88.     }
  89.  
  90.     if (!(ap.ap_Flags & APF_ITSWILD) && (err == ERROR_OBJECT_NOT_FOUND))
  91.         createfile(pat);
  92.     else if (err != ERROR_NO_MORE_ENTRIES)    /* abnormal error */
  93.         PrintFault(err, "touch");
  94.  
  95.     MatchEnd(&ap);
  96. }
  97.  
  98.